const fs = require('fs');


// 读取流
/* const readStream = fs.createReadStream("./test.txt");
let str = "";
let count = 0;

readStream.setEncoding("UTF8");

readStream.on("data", (data) => {
  str += data;
  count ++;
})

readStream.on("end", () => {
  console.log(str);
  console.log(count);
})

readStream.on("error", (err) => {
  console.log(err);
}) */



// 写入流
/* const writeStream = fs.createWriteStream("./test1.txt");
const data = "本教程会为大家介绍常用的流操作。";

writeStream.write(data, "UTF8");

writeStream.end();

writeStream.on("finish", () => {
  console.log("写入成功");
})

writeStream.on("error", (err) => {
  console.log(err);
}) */


// 管道流
const readStream = fs.createReadStream("./test.txt");
const writeStream = fs.createWriteStream("./stream/test.txt");
// 管道读写操作
readStream.pipe(writeStream);